﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;

namespace DMSClient.General
{
    public partial class ChangePass : Form
    {
        private string usuario = "";

        public ChangePass()
        {
            InitializeComponent();
        }
        public ChangePass(string user)
        {
            InitializeComponent();
            usuario = user;
        }

        private void ultraTextEditor1_TextChanged(object sender, EventArgs e)
        {
            if(uteConfirmNewPass.Text.Equals(uteNewPass.Text)){
                lErrorCoincidencia.Visible = false;
            }else{
                lErrorCoincidencia.Visible = true;
            }
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            if (uteOldPass.Text.Equals("")) {
                MessageBox.Show(this, "You should write the old password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                uteOldPass.Focus();
                return;
            }
            if (uteNewPass.Text.Equals(""))
            {
                MessageBox.Show(this, "You should write the new password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                uteNewPass.Focus();
                return;
            }
            if (lErrorCoincidencia.Visible) {
                MessageBox.Show(this, "Password mismatch", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                uteConfirmNewPass.Focus();
                return;
            }

            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 1, 0);
            binding.Security.Mode = SecurityMode.None;

            int validUser = -1;

            try
            {
                EndpointAddress endpoint = new EndpointAddress(Properties.Settings.Default.Server);
                DMSServices.DMSServicesClient UserSrv = new DMSServices.DMSServicesClient(binding, endpoint);

                UserSrv.Open();
                validUser = UserSrv.validateUser(usuario, uteOldPass.Text);
                UserSrv.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "Error establishing connection to the server", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (validUser >= 1)
            {
                int cambiado = 0;
                try
                {
                    EndpointAddress endpoint = new EndpointAddress(Properties.Settings.Default.Server);
                    DMSServices.DMSServicesClient UserSrv = new DMSServices.DMSServicesClient(binding, endpoint);

                    UserSrv.Open();
                    cambiado = UserSrv.setPassword(usuario, uteNewPass.Text);
                    UserSrv.Close();
                    if (cambiado > 0)
                    {
                        MessageBox.Show(this, "Password changed successfully", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else {
                        MessageBox.Show(this, "Error to change the password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception exc) {
                    MessageBox.Show(this, "Error to change the password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else if (validUser == 0)
            {
                MessageBox.Show(this, "Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                uteOldPass.Focus();
            }
            else
            {
                MessageBox.Show(this, "Server side error", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }


        }
    }
}
